#include "losu.h"
#include "losu_bytecode.h"
#include "losu_dump.h"
#include "losu_gc.h"
#include "losu_mem.h"
#include "losu_object.h"
#include "losu_syntax.h"
#include "losu_vm.h"

#include "list.h"
#include "collections.h"

/** losu-capi (self) -> self
 * @brief: "constructor of class: THISxkit_MODULEcollections_CLASSList"
 */
int32_t THISxkit_MODULEcollections_CLASSList_constructor(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    ListHeader *h = ListInit(vm);
    obj_unitsetstr(vm, *argv, "__cptr__", obj_newptr(vm, (void*)h));
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self, index: number, item: any) -> self
 * @brief: "在指定索引插入元素,越界引发OutOfIndex错误"
 */
int32_t THISxkit_MODULEcollections_CLASSList_FUNCinsert(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 3) {
        __error_TooFewArguments(vm);
        return 0;
    }
    ListHeader *h = getCPointer(vm, argv);
    size_t index = obj_getnum(vm, argv + 1);
    ListInsert(vm, h, index, argv + 2);
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self) -> 
 * @brief: "析构函数"
 */
int32_t THISxkit_MODULEcollections_CLASSList_FUNC__del__(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    ListHeader *h = getCPointer(vm, argv);
    ListFree(vm, h);
    return 0;
}


/** losu-capi (self, item: any) -> self
 * @brief: "在链表尾部添加元素"
 */
int32_t THISxkit_MODULEcollections_CLASSList_FUNCaddLast(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 2) {
        __error_TooFewArguments(vm);
    }
    ListHeader *h = getCPointer(vm, argv);
    ListAddLast(vm, h, argv + 1);
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self) -> number
 * @brief: "返回链表大小"
 */
int32_t THISxkit_MODULEcollections_CLASSList_FUNCsize(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    (void)argv;
    ListHeader *h = getCPointer(vm, argv);
    return arg_returnNum(vm, ListSize(vm, h));
}


/** losu-capi (self, index: number) -> Any
 * @brief: "返回指定索引的元素,越界引发OutOfIndex错误"
 */
int32_t THISxkit_MODULEcollections_CLASSList_FUNCget(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 2) {
        __error_TooFewArguments(vm);
        return 0;
    }
    ListHeader *h = getCPointer(vm, argv);
    size_t index = obj_getnum(vm, argv + 1);
    losu_object target;
    ListGet(vm, h, index, &target);
    stack_push(vm, target);
    return 1;
}


/** losu-capi (self, item: any) -> self
 * @brief: "在链表头部添加元素"
 */
int32_t THISxkit_MODULEcollections_CLASSList_FUNCaddFirst(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 2) {
        __error_TooFewArguments(vm);
    }
    ListHeader *h = getCPointer(vm, argv);
    ListAddFirst(vm, h, argv + 1);
    stack_push(vm, *argv);
    return 1;
}


/** losu-capi (self) -> self
 * @brief: "删除并返回头部元素"
 */
int32_t THISxkit_MODULEcollections_CLASSList_FUNCremoveFirst(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    ListHeader *h = getCPointer(vm, argv);
    losu_object target;
    ListRmFirst(vm, h, &target);
    stack_push(vm, target);
    return 1;
}


/** losu-capi (self) -> self
 * @brief: "删除并返回尾部元素"
 */
int32_t THISxkit_MODULEcollections_CLASSList_FUNCremoveLast(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    (void)argc;
    ListHeader *h = getCPointer(vm, argv);
    losu_object target;
    ListRmLast(vm, h, &target);
    stack_push(vm, target);
    return 1;
}


/** losu-capi (self, index: number) -> self
 * @brief: "删除指定索引的元素,越界引发OutOfIndex错误"
 */
int32_t THISxkit_MODULEcollections_CLASSList_FUNCremove(losu_vm_t vm, int32_t argc, losu_object argv[]) {
    if (argc < 2) {
        __error_TooFewArguments(vm);
        return 0;
    }
    ListHeader *h = getCPointer(vm, argv);
    size_t index = obj_getnum(vm, argv + 1);
    ListRemove(vm, h, index);
    stack_push(vm, *argv);
    return 1;
}
